﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microhard.WeixinMP.Unities
{
    public static class ObjectConverterExtension
    {
        public static DateTime BaseTime = new DateTime(1970, 1, 1);//Unix起始时间

        /// <summary>
        /// 将微信时间转化为datetime
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DateTime GetDateTimeFromXml(this string value)
        {
            var longDateTime = long.Parse(value);
            return BaseTime.AddTicks((longDateTime + 8 * 60 * 60) * 10000000);
        }

        /// <summary>
        /// 将datetime转化为微信时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static long CreateWeixinDateTime(this DateTime dateTime)
        {
            return (dateTime.Ticks - BaseTime.Ticks) / 10000000 - 8 * 60 * 60;
        }
    }
}
